(function() {

	// 获取显示页面数据 服务器分页
	var serverData = {};
	serverData.key='';
	var serverTable;
	jQuery(document).ready(function() {serverTable = jQuery('#dataWeeklyTable').DataTable(
										{
											"serverSide" : true,
											"ordering" : false,
											"ajax" : {
												"dataType" : "json",
												"type" : "get",
												"cache" : false,
												"url" : window.rootPath
														+ '/ui/weekly/getpage1',
												"data" : function(d) {
													return $.extend({}, d,
															serverData);
												}
											},
											columns : [
													{
														data : null,
														defaultContent : ""// 排序
													},
												{
													data: null,
													defaultContent: "",
													render: function (data, type, full, meta) {
														var operate ='<input type="checkbox" class="checkchild">';
														return operate;
													}
												},
												{
													data: null,
													render: function (data, type, full, meta) {
														var dOut;
														if(data.published==false){
															dOut = '<td class="center" ><span class="badge label-pill label-danger">未提交</span></td>';
														}
														else if(data.published==true) {
															dOut = '<td class="center" ><span class="badge label-pill label-primary">已提交</span></td>';
														}
														return dOut;
													}
												},
													{
														data : 'issuer.empLogin.userName'
													},
													{
														data : 'issueDate',
														render : function(data,
																type, full,
																meta) {
															if (data) {return moment(data).format("YYYY-MM-DD");
															}
														}
													},
													{
														data:'issuer.saleArea.superiorArea'
													},
													{
														data : 'name'
													},
													{
														data : 'instruction'
													},
													{
														data : 'memo'
													},
													
													],
											"drawCallback" : function(oSettings) {
												var k = 0;
												var total = oSettings.aiDisplay.length
														+ oSettings._iDisplayStart;

												for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
													$(
															'td:eq(0)',
															oSettings.aoData[oSettings.aiDisplay[k]].nTr)
															.text(i + 1);
													k++;
												}
											}
										});

						///列表中的全选
						$("#checkAll").click(function(){
							var check = $(this).prop("checked");
							$(".checkchild").prop("checked", check);
						});
						// 添加销售人员属性，弹窗清空
						jQuery('.addBtn').on('click', function() {
							jQuery('#id').val('');						
							jQuery('#weeklyName').val('');							
							jQuery('.week').val('');
							jQuery('.contents').val('');
							jQuery('#memo').val('');
							jQuery("#instruction").val('');
							jQuery('#weeklyModal').modal('show');
						});

						//编辑周报信息
						jQuery('.editBtn').on('click', function () {
							var selects=getSelect("#dataWeeklyTable",1);
							if(selects.length>1){
								$.springLemonDialog({
									content: '只能编辑一条记录',// 确认文本
									clickButton: function (sender, modal, index) {// 按钮事件处理
										$(this).closeDialog(modal);
									}
								});
							}
							else if(selects.length==0) {
								$.springLemonDialog({
									content: '请选择要编辑的记录',// 确认文本
									clickButton: function (sender, modal, index) {// 按钮事件处理
										$(this).closeDialog(modal);
									}
								});
							}else if(selects.length==1) {
								if (selects[0].published == 0) {
									jQuery('#id').val(selects[0].id);
									jQuery('#weeklyName').val(selects[0].name);
									if(selects[0].week1){
										jQuery("#week1").val(moment(selects[0].week1).format("YYYY-MM-DD"));
									}else{
										jQuery("#week1").val('');
									}
									if(selects[0].week2){
										jQuery("#week2").val(moment(selects[0].week2).format("YYYY-MM-DD"));
									}else{
										jQuery("#week2").val('');
									}
									
									if(selects[0].week3){
										jQuery("#week3").val(moment(selects[0].week3).format("YYYY-MM-DD"));
									}else{
										jQuery("#week3").val('');
									}
									
									if(selects[0].week4){
										jQuery("#week4").val(moment(selects[0].week4).format("YYYY-MM-DD"));
									}else{
										jQuery("#week4").val('');
									}
									
									if(selects[0].week5){
										jQuery("#week5").val(moment(selects[0].week5).format("YYYY-MM-DD"));
									}else{
										jQuery("#week5").val('');
									}
									
									if(selects[0].week6){
										jQuery("#week6").val(moment(selects[0].week6).format("YYYY-MM-DD"));
									}else{
										jQuery("#week6").val('');
									}
									
									if(selects[0].week7){
										jQuery("#week7").val(moment(selects[0].week7).format("YYYY-MM-DD"));
									}else{
										jQuery("#week7").val('');
									}
									jQuery('#content1').val(selects[0].content1);
									jQuery('#content2').val(selects[0].content2);
									jQuery('#content3').val(selects[0].content3);
									jQuery('#content4').val(selects[0].content4);
									jQuery('#content5').val(selects[0].content5);
									jQuery('#content6').val(selects[0].content6);
									jQuery('#content7').val(selects[0].content7);
									if(selects[0].nextTime){
										jQuery('#nextTime').val(moment(selects[0].nextTime).format('YYYY-MM-DD'));
									}else{
										jQuery('#nextTime').val('');
									}
									jQuery('#workPlan').val(selects[0].workPlan);
									jQuery('#memo').val(selects[0].memo);
									jQuery('#instruction').val(selects[0].instruction);
									jQuery("#published").val(selects[0].published);
									jQuery('#weeklyModal').modal('show');
								}
							else {
									message("已提交不可修改")
								}
							};
						});
						// 添加/编辑周报内容，保存
						var $btn = jQuery('.saveBtn').on('click',
										function() {
											if (jQuery("#weeklyEditForm")
													&& !jQuery(
															"#weeklyEditForm")
															.Validform(
																	{
																		tiptype : 3,
																		showAllError : true
																	}).check(
																	false)) {
												return;
											}
											;
											    var isId = jQuery("#id").val();
												var name = jQuery('#weeklyName').val();											
												var week1=jQuery('#week1').val();
												var week2=jQuery('#week2').val();
												var week3=jQuery('#week3').val();
												var week4=jQuery('#week4').val();
												var week5=jQuery('#week5').val();
												var week6=jQuery('#week6').val();
												var week7=jQuery('#week7').val();
												var content1 = jQuery('#content1').val();
												var content2 = jQuery('#content2').val();
												var content3 = jQuery('#content3').val();
												var content4 = jQuery('#content4').val();
												var content5 = jQuery('#content5').val();
												var content6 = jQuery('#content6').val();
												var content7 = jQuery('#content7').val();
												var nextTime=jQuery("#nextTime").val();
												var workPlan=jQuery("#workPlan").val();
												var memo = jQuery('#memo').val();
												var instruction=jQuery('#instruction').val();
												var published=jQuery("#published").val();
												// 判断是编辑还是新增
												if (isId == "") {
												var api = window.rootPath+ "/ui/weekly";
												var d = {											
													"name" : name,
													"week1":week1,
													"week2":week2,
													"week3":week3,
													"week4":week4,
													"week5":week5,
													"week6":week6,
													"week7":week7,
													"content1" : content1,
													"content2" : content2,
													"content3" : content3,
													"content4" : content4,
													"content5" : content5,
													"content6" : content6,
													"content7" : content7,
													"nextTime":nextTime,
													"workPlan":workPlan,
													"memo" : memo,
													"instruction":instruction,
													"published":false
												};
												jQuery(".btn").attr("disabled", true);
												jQuery.ajax({
															type : 'put',
															url : api,
															data : JSON.stringify(d),
															dataType : 'json',
															contentType : 'application/json;charset=utf-8',
															success : function(data) {
																jQuery(".btn").attr("disabled", false);
																jQuery('#weeklyModal').modal('hide');
																serverTable.draw(false);
															}
														});

											} else {
													var data = {
															"id":isId,
															"name" : name,
															"week1":week1,
															"week2":week2,
															"week3":week3,
															"week4":week4,
															"week5":week5,
															"week6":week6,
															"week7":week7,
															"content1" : content1,
															"content2" : content2,
															"content3" : content3,
															"content4" : content4,
															"content5" : content5,
															"content6" : content6,
															"content7" : content7,
															"nextTime":nextTime,
															"workPlan":workPlan,
															"memo" : memo,
															"instruction":instruction,
															"published":published
													};
												
												var url=window.rootPath+'/ui/weekly';
												jQuery(".btn").attr("disabled", true);
												jQuery.ajax({
														url: encodeURI(url),
														type: 'patch',
														data: JSON.stringify(data),
														dataType: 'json',
														contentType: 'application/json;charset=utf-8',
														success: function () {
															jQuery(".btn").attr("disabled", false);
															jQuery('#weeklyModal').modal('hide');
															serverTable.draw(false);
														}

													});


											}
										});

						// 查询
						jQuery('.searchBtn').on('click', function() {
							serverData.key = jQuery('#keyword').val();
							serverTable.draw();
						})
						//提交
						jQuery('.submit').on('click',function(){
							var selects=getSelect("#dataWeeklyTable",1);
							var id=selects[0].id;
							var data = {
								id:id
							}
							if( selects[0].published==1){
								message("不可重复提交");
							}
							else {
							var url = window.rootPath
								+ '/ui/weekly/submit'+"?weeklyId="+id;
							jQuery
								.ajax({
									url : encodeURI(url),
									type : 'post',
									data : JSON.stringify(data),
									dataType : 'json',
									contentType : 'application/json;charset=utf-8',
									success : function() {
										message("提交成功");
										serverTable.draw();
									}

								});
							}
						});
						//查看周报
						jQuery('.checkBtn').click(function(){
							var selects=getSelect("#dataWeeklyTable",1);
							if(selects.length>1){
								$.springLemonDialog({
									content: '只能查看一条周报',// 确认文本
									clickButton: function (sender, modal, index) {// 按钮事件处理
										$(this).closeDialog(modal);
									}
								});
							}
							else if(selects.length==0) {
								$.springLemonDialog({
									content: '请选择要查看的周报',// 确认文本
									clickButton: function (sender, modal, index) {// 按钮事件处理
										$(this).closeDialog(modal);
									}
								});
							}else if(selects.length==1) {
									jQuery('#check-weeklyName').val(selects[0].name);
									if(selects[0].week1){
										jQuery("#check-week1").val(moment(selects[0].week1).format("YYYY-MM-DD"));
									}else{
										jQuery("#check-week1").val('');
									}
									if(selects[0].week2){
										jQuery("#check-week2").val(moment(selects[0].week2).format("YYYY-MM-DD"));
									}else{
										jQuery("#check-week2").val('');
									}
									
									if(selects[0].week3){
										jQuery("#check-week3").val(moment(selects[0].week3).format("YYYY-MM-DD"));
									}else{
										jQuery("#check-week3").val('');
									}
									
									if(selects[0].week4){
										jQuery("#check-week4").val(moment(selects[0].week4).format("YYYY-MM-DD"));
									}else{
										jQuery("#check-week4").val('');
									}
									
									if(selects[0].week5){
										jQuery("#check-week5").val(moment(selects[0].week5).format("YYYY-MM-DD"));
									}else{
										jQuery("#check-week5").val('');
									}
									
									if(selects[0].week6){
										jQuery("#check-week6").val(moment(selects[0].week6).format("YYYY-MM-DD"));
									}else{
										jQuery("#check-week6").val('');
									}
									
									if(selects[0].week7){
										jQuery("#check-week7").val(moment(selects[0].week7).format("YYYY-MM-DD"));
									}else{
										jQuery("#check-week7").val('');
									}
									jQuery('#check-content1').val(selects[0].content1);
									jQuery('#check-content2').val(selects[0].content2);
									jQuery('#check-content3').val(selects[0].content3);
									jQuery('#check-content4').val(selects[0].content4);
									jQuery('#check-content5').val(selects[0].content5);
									jQuery('#check-content6').val(selects[0].content6);
									jQuery('#check-content7').val(selects[0].content7);
									if(selects[0].nextTime){
										jQuery('#check-nextTime').val(moment(selects[0].nextTime).format('YYYY-MM-DD'));
									}else{
										jQuery('#check-nextTime').val('');
									}
									jQuery('#check-workPlan').val(selects[0].workPlan);
									jQuery('#check-memo').val(selects[0].memo);
									jQuery('#check-instruction').val(selects[0].instruction);
									jQuery('#ckeckWeeklyModal').modal('show');
							};
						});
						// 撤销查询
						jQuery('.resetBtn').on("click", function() {
							jQuery('#keyword').val('');
							serverData.key = '';
							serverTable.draw();
						});
                        //删除周报
						jQuery('.delBtn').on('click', function () {
							var selects=getSelect("#dataWeeklyTable",1);
							var url=window.rootPath+"/ui/weekly/delWeekly";
							delWeeklySelect(selects,url,serverTable)
						});
						function delWeeklySelect(selects,url,tableName){
							if(selects.length==0){
								$.springLemonDialog({
									content: '请选择删除的记录',// 确认文本
									clickButton: function (sender, modal, index) {// 按钮事件处理
										$(this).closeDialog(modal);
									}
								});
							}else{
								var ids=selects[0].id;
								var operate ="0";
								for(var i=0;i<selects.length;i++){
									if(selects[i].published =="1"){
										operate ="1";
									}

								}
								if(operate == "1"){
									$.springLemonDialog({
										content: '已发布信息不可删除！',// 确认文本
										clickButton: function (sender, modal, index) {// 按钮事件处理
											$(this).closeDialog(modal);
										}
									});
								}
								else{
									for(var i=1;i<selects.length;i++){
										ids += ',' + selects[i].id;
									}
									var api=url+"?ids="+ids;
									$.springLemonDialog({
										content: '确认删除'+selects.length+'条记录？',// 确认文本
										clickButton: function (sender, modal, index) {// 按钮事件处理
											if (index == 0) {
												jQuery.ajax({
													url:encodeURI(api),
													cache:false,
													type :"get",
													dataType:"json",
													contentType:"application/json;charset=utf-8",//解决返回数据中的中文乱码问题
													success:function(data){

														serverTable.draw(false);
													}
												});
											}
											$(this).closeDialog(modal);
										}
									});
								}

							}

						}
			});
	
//	////批示
//	jQuery(".instructBtn").click(function(){
//		var selects=getSelect("#dataWeeklyTable",1);
//		if(selects.length>1){
//			$.springLemonDialog({
//				content: '只能批示一条记录',// 确认文本
//				clickButton: function (sender, modal, index) {// 按钮事件处理
//					$(this).closeDialog(modal);
//				}
//			});
//		}
//		else if(selects.length==0) {
//			$.springLemonDialog({
//				content: '请选择要批示的记录',// 确认文本
//				clickButton: function (sender, modal, index) {// 按钮事件处理
//					$(this).closeDialog(modal);
//				}
//			});
//		}else if(selects.length==1) {
//			console.log(selects[0].issuer.id)
//			var url=window.rootPath+'/ui/weekly/validate/'+selects[0].issuer.id;
//			jQuery.ajax({
//				url:encodeURI(url),
//				cache:false,
//				type:'get',
//				dataType:'json',
//				success:function(data){
//					if (selects[0].published == true&&data.code!=0) {
//						jQuery('#weeklyName').prop("disabled",true);
//						jQuery('.week').prop("disabled",true);
//						jQuery('.contents').prop("disabled",true);
//						jQuery('#weeklyName').prop("disabled",true);
//						jQuery('#memo').prop("disabled",true);
//						jQuery(".instruction").prop("disabled",false);
//						
//						jQuery('#id').val(selects[0].id);
//						jQuery('#weeklyName').val(selects[0].name);
//						if(selects[0].week1){
//							jQuery("#week1").val(moment(selects[0].week1).format("YYYY-MM-DD"));
//						}else{
//							jQuery("#week1").val('');
//						}
//						if(selects[0].week2){
//							jQuery("#week2").val(moment(selects[0].week2).format("YYYY-MM-DD"));
//						}else{
//							jQuery("#week2").val('');
//						}
//						
//						if(selects[0].week3){
//							jQuery("#week3").val(moment(selects[0].week3).format("YYYY-MM-DD"));
//						}else{
//							jQuery("#week3").val('');
//						}
//						
//						if(selects[0].week4){
//							jQuery("#week4").val(moment(selects[0].week4).format("YYYY-MM-DD"));
//						}else{
//							jQuery("#week4").val('');
//						}
//						
//						if(selects[0].week5){
//							jQuery("#week5").val(moment(selects[0].week5).format("YYYY-MM-DD"));
//						}else{
//							jQuery("#week5").val('');
//						}
//						
//						if(selects[0].week6){
//							jQuery("#week6").val(moment(selects[0].week6).format("YYYY-MM-DD"));
//						}else{
//							jQuery("#week6").val('');
//						}
//						
//						if(selects[0].week7){
//							jQuery("#week7").val(moment(selects[0].week7).format("YYYY-MM-DD"));
//						}else{
//							jQuery("#week7").val('');
//						}
//						jQuery('#content1').val(selects[0].content1);
//						jQuery('#content2').val(selects[0].content2);
//						jQuery('#content3').val(selects[0].content3);
//						jQuery('#content4').val(selects[0].content4);
//						jQuery('#content5').val(selects[0].content5);
//						jQuery('#content6').val(selects[0].content6);
//						jQuery('#content7').val(selects[0].content7);
//						if(selects[0].nextTime){
//							jQuery('#nextTime').val(moment(selects[0].nextTime).format('YYYY-MM-DD'));
//						}else{
//							jQuery('#nextTime').val('');
//						}
//						jQuery('#workPlan').val(selects[0].workPlan);
//						jQuery('#memo').val(selects[0].memo);
//						jQuery('#instruction').val(selects[0].instruction);
//						jQuery('#weeklyModal').modal('show');
////						jQuery('#instruct-weeklyName').val(selects[0].name);
////						if(selects[0].week1){
////							jQuery("#instruct-week1").val(moment(selects[0].week1).format("YYYY-MM-DD"));
////						}else{
////							jQuery("#instruct-week1").val('');
////						}
////						if(selects[0].week2){
////							jQuery("#instruct-week2").val(moment(selects[0].week2).format("YYYY-MM-DD"));
////						}else{
////							jQuery("#instruct-week2").val('');
////						}
////						
////						if(selects[0].week3){
////							jQuery("#instruct-week3").val(moment(selects[0].week3).format("YYYY-MM-DD"));
////						}else{
////							jQuery("#instruct-week3").val('');
////						}
////						
////						if(selects[0].week4){
////							jQuery("#instruct-week4").val(moment(selects[0].week4).format("YYYY-MM-DD"));
////						}else{
////							jQuery("#instruct-week4").val('');
////						}
////						
////						if(selects[0].week5){
////							jQuery("#instruct-week5").val(moment(selects[0].week5).format("YYYY-MM-DD"));
////						}else{
////							jQuery("#instruct-week5").val('');
////						}
////						
////						if(selects[0].week6){
////							jQuery("#instruct-week6").val(moment(selects[0].week6).format("YYYY-MM-DD"));
////						}else{
////							jQuery("#instruct-week6").val('');
////						}
////						
////						if(selects[0].week7){
////							jQuery("#instruct-week7").val(moment(selects[0].week7).format("YYYY-MM-DD"));
////						}else{
////							jQuery("#instruct-week7").val('');
////						}
////						jQuery('#instruct-content1').val(selects[0].content1);
////						jQuery('#instruct-content2').val(selects[0].content2);
////						jQuery('#instruct-content3').val(selects[0].content3);
////						jQuery('#instruct-content4').val(selects[0].content4);
////						jQuery('#instruct-content5').val(selects[0].content5);
////						jQuery('#instruct-content6').val(selects[0].content6);
////						jQuery('#instruct-content7').val(selects[0].content7);
////						if(selects[0].nextTime){
////							jQuery('#instruct-nextTime').val(moment(selects[0].nextTime).format('YYYY-MM-DD'));
////						}else{
////							jQuery('#instruct-nextTime').val('');
////						}
////						jQuery('#instruct-workPlan').val(selects[0].workPlan);
////						jQuery('#instruct-memo').val(selects[0].memo);
////						jQuery('#instruct-instruction').val(selects[0].instruction);
////						jQuery('#instructWeeklyModal').modal('show');
//					}else{
//						message("不能批示自己")
//					}
//				}
//			})
//		};
//	});
	///excel导出
	jQuery('.btnExport').on("click",function() {
		var selects=getSelect("#dataWeeklyTable",1);
		if(selects.length>0){
			var key=selects[0].id;
			for(var i=1;i<selects.length;i++){
				   key += ',' + selects[i].id;  
			 }
			   var url=window.rootPath+ "/ui/weekly/exportSelects/"+key;
			   window.location = url
			
		}else{
			var key = jQuery('#keyword').val();
			if (!key)
				key = 'null';
			var url = window.rootPath+ "/ui/weekly/export/" + key;
			window.location = url;
		}
	});
})();
